﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CourierManager
{
    public class ValidatorLicensePlates
    {
        public static void ValidateLicensePlates(string licensePlate)
        {
            if (licensePlate.Length != 7 && licensePlate.Length != 8)
            {
                throw new ArgumentException();
            }

            string regionSubStr = licensePlate.Substring(0, 2);
            if (licensePlate.Length == 7)
            {
                regionSubStr = licensePlate.Substring(0, 1);
            }
            ValidateLetters(regionSubStr);

            string numbers = licensePlate.Substring(2, 4);
            if (licensePlate.Length == 7)
            {
                numbers = licensePlate.Substring(1, 4);
            }
            foreach (char digit in numbers)
            {
                if (!Char.IsDigit(digit))
                {
                    throw new ArgumentException();
                }
            }

            string lastSubStr = licensePlate.Substring(6);
            if (licensePlate.Length == 7)
            {
                lastSubStr = licensePlate.Substring(5);
            }
            ValidateLetters(lastSubStr);

        }

        private static void ValidateLetters(string subString)
        {
            char[] arrayLettersLatin = new char[] { 'A', 'B', 'C', 'E', 'H', 'K', 'M', 'O', 'P', 'T', 'X', 'Y' };   //Latin input

            char[] arrayLettersCyr = new char[] { 'А', 'В', 'С', 'Е', 'Н', 'К', 'М', 'О', 'Р', 'Т', 'Х', 'У' };     //Cyrillic input

            for (int i = 0; i < subString.Length; i++)
            {
                if (subString[i] <= 89)
                {
                    ValidateChar(subString[i], arrayLettersLatin);
                }
                else
                {
                    ValidateChar(subString[i], arrayLettersCyr);
                }
            }
        }

        private static void ValidateChar(char arg, char[] collection)
        {
            bool check = false;
            for (int i = 0; i < collection.Length; i++)
            {
                if (collection[i] == arg)
                {
                    check = true;
                    break;
                }            
            }
            if (check == false)
            {
                throw new ArgumentException();
            }
        }
    }
}
